# Definition for singly-linked list.
class ListNode:
    def __init__(self, x):
        self.val = x
        self.next = None


class Solution:
    def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
        head = ListNode(0)
        next_num, jin_wei = head, 0
        while l1 or l2:
            if l1:
                x = l1.val
                l1 = l1.next
            else:
                x = 0
            if l2:
                y = l2.val
                l2 = l1.next
            else:
                y = 0
            mid_num = x + y + jin_wei
            next_num.next = ListNode(mid_num % 10)
            next_num = next_num.next
            jin_wei = mid_num // 10
        if jin_wei:
            next_num.next = ListNode(jin_wei)
        return head.next
